﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CheckClicker;
using System.Data.OleDb;

namespace CheckClicker
{
    public partial class frmSetting : Form
    {
        CommunicationManager comm = new CommunicationManager();
        string transType = string.Empty;

        private OleDbConnection objConnect;
        private DataTable objDataTable;
        private enum varMode : int { AddMode, EditMode, DeleteMode }
        

        #region "Tạo Kết nối và hủy kết nối"

        private void subCreateConnect()
        {
            String varChuoiConnect = "Provider = Microsoft.Jet.OLEDB.4.0 ;" +
                        "Data Source =" + Application.StartupPath + @"\Database.mdb;" +
                        "User ID = Admin";
            objConnect = new OleDbConnection(varChuoiConnect);
            objConnect.Open(); //Mở kết nối
        }

        private void subDestroyConnect()
        {
            objConnect.Close(); //Đóng kết nối
            objConnect.Dispose();//Giải phóng tài nguyên
            objConnect = null; //Hủy đối tượng
        }
        #endregion
        #region "Add du lieu"
        private void subUpdateByAdapterCommandBuilder(string name, string parity, string stop, string data, string baud)
        {
            //Tạo kết nối tới file Access
            subCreateConnect();
            //Tạo đối tượng command
            OleDbCommand objCommand = new OleDbCommand();
            objCommand.Connection = objConnect;
            objCommand.CommandType = CommandType.Text;
            objCommand.CommandText = "Update Comport Set " +
                                    "Name = " + "'" + name + "'" + "," +
                                    "Parity = " + "'" + parity + "'" + "," +
                                    "Stop = " + "'" + stop + "'" + "," +
                                    "Data = " + "'" + data + "'" + "," +
                                    "Baud = " + "'" + baud + "'" +
                                    "Where STT = " + 1;
            objCommand.ExecuteNonQuery();
            //Hủy đối tượng
            objCommand.Dispose();
            objCommand = null;
            subDestroyConnect();
        }
        #endregion
        
        public frmSetting()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            subUpdateByAdapterCommandBuilder(cboPort.Text, cboParity.Text, cboStop.Text, cboData.Text, cboBaud.Text);
            comm.PortName = cboPort.Text;
            comm.Parity = cboParity.Text;
            comm.StopBits = cboStop.Text;
            comm.DataBits = cboData.Text;
            comm.BaudRate = cboBaud.Text;
            this.Hide();
            //frmMain frm = new frmMain();
            //frm.ShowDialog();
            
        }

        private void frmSetting_Load(object sender, EventArgs e)
        {
            LoadValues();
            SetDefaults();
        }

        private void SetDefaults()
        {
            cboPort.SelectedText = "COM2";
            cboBaud.SelectedText = "9600";
            cboParity.SelectedIndex = 0;
            cboStop.SelectedIndex = 1;
            cboData.SelectedIndex = 1;
        }

        /// <summary>
        /// methos to load our serial
        /// port option values
        /// </summary>
        private void LoadValues()
        {
            comm.SetPortNameValues(cboPort);
            comm.SetParityValues(cboParity);
            comm.SetStopBitValues(cboStop);
        }

        /// <summary>
        /// method to set the state of controls
        /// when the form first loads
        /// </summary>
       
    }
}
